var $gridList = $('#grid');
var editDialog, $top = $('#top');//顶部栏区域
var $ecuTypeCon = $('#ecuTypeCon');//ecu节点容器

/**
 * 加载ECU类型列表
 */
function loadECUTypleList(fn) {
    $.ajaxExt({
        url: "/reportServiceManual/getEcuTypeInfoList.htm"
    }).done(function (data) {
        if (data) {
            $.each(data, function (i, item) {
                var $li = $('<li><a>' + item.ecuMftName + '</li></a>');
                $li.data('data-val', item.ecuMftCode);
                $ecuTypeCon.append($li);
            });
            fn && fn();
        }
    });
}

$(window).on('resize', function () {
    var height = $(window).height();
    $ecuTypeCon.height(height - 70);
    var $parent = $gridList.closest('.table-box');
    $parent.height(height - 70);
    if ($gridList.children().length) {
        $gridList.jqGrid('setGridWidth', $parent.width() - 2);
        $gridList.jqGrid('setGridHeight', height - 137);
    }
}).trigger('resize');


require(['grid', 'topBar', 'layout'], function () {
    var findMftByMftName = $("#findMftByMftName");
    var postData = {};
    //$('body').layout();
    $top.initForm();

    loadECUTypleList(function () {
        $('li:eq(0)', $ecuTypeCon).trigger('click');
    });

    //点击左侧ECU节点代码查找故障列表
    $ecuTypeCon.on('click', 'li', function () {
        $(this).addActiveClass();
        postData = $top.getValues();
        postData.ecuMftCode = $(this).data('data-val');
        reloadGridData();
    });

    //根据故障名称查找
    findMftByMftName.click(function () {
        var $activeLi = $('li.active', $ecuTypeCon);
        if ($activeLi.length) {
            postData = $top.getValues();
            postData.ecuMftCode = $activeLi.data('data-val');
            reloadGridData();
        }
    });

    $gridList.grid({
        url: "/reportServiceManual/getMftInfoList.htm",
        multiselect: false,
        showPager: true,
        colModel: [
            {name: "corpId", label: 'corpId', hidden: true},
            {name: "mftTypeCode", label: 'mftTypeCode', hidden: true},
            {name: "mftDiscCode", label: 'mftDiscCode', hidden: true},
            {name: "corpName", label: '所属企业'},
            {name: "ecuMftName", label: 'ECU节点'},
            {name: "mftTypeName", label: '故障名称'},
            {name: "mftGradeName", label: '故障等级'},
            {name: "mftClCode", label: '故障分类', dict: "MFTC0000"},
            {name: "mftTypeCode", label: '故障编码'},
            {name: "mftInsideCode", label: '故障闪码'},
            {name: "mftAnl", label: '故障分析'},
            {name: "wthrFilter", label: '是否过滤',formatter:function(cellValue){
            	if(cellValue=='1'){
            		return '是';
            	}else{
            		return '否';
            	}
            }},
            {name: "solutionPlan", label: '故障方案'},
            {
                name: "operate", width: 130, label: '操作', title: false,align: "center",
                formatter: 'operArea',
                formatoptions: [{
                    text: '编辑',
                    css: 'operate-edit',
                    click: function (data) {
                        getEditDialog('edit', '编辑').dialog('exec', 'setVal', data.corpId, data.mftTypeCode, data.mftDiscCode);
                    }
                }]

            }
        ]
    });

    function getEditDialog(option) {
        option = option || {};
        if (editDialog) {
            editDialog.dialog('open');
            if (option.title) {
                editDialog.dialog('setOption', 'title', option.title);
            }
        } else {
            editDialog = $('<div>').dialog(
                {
                    title: option.title || "编辑故障信息",
                    url: './mainte/report/reportServiceManualEdit.htm',
                    height: 450,
                    width: 700,
                    autoOpen: true,
                    buttons: [
                        {
                            text: '保存',
                            css: 'mr10',
                            click: function () {
                                var _t = this;
                                $(this).dialog('exec', 'add', function () {
                                    $(_t).dialog('close');
                                    reloadGridData();
                                });
                            }
                        }, {
                            text: '取消',
                            css: 'btn-cancel',
                            click: function () {
                                $(this).dialog('close');
                            }
                        }]
                });
        }
        return editDialog;
    }

    //重新查询维保信息grid
    var reloadGridData = function () {
        $gridList.grid('setGridParam', {
            datatype: "JSON",
            postData: postData
        }).trigger("reloadGrid");
    };
});